<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="scoped">
                  <el-button size="small" type="success" @click="assignPerm(scoped.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(scoped.row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(scoped.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <!-- 放置分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height:60px">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="fromData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="fromData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="fromData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="fromData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog v-if="showDialog" :title="text" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <!-- <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        /> -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="{label:'name'}"
          :default-expand-all="true"
          :show-checkbox="true"
          :check-strictly="true"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, deleteRole, updateRole, getRoleDetail, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { listToTreeData } from '@/utils/index'
export default {
  data() {
    return {
      userId: '',
      permData: [],
      showPermDialog: false,
      fromData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      list: [],
      showDialog: false,
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    text() {
      return this.roleForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    // 渲染公司信息
    this.getCompanyInfo()
    // 渲染角色列表
    this.getRoleList()
  },
  methods: {
    // 点击分配权限按钮
    async assignPerm(id) {
      this.userId = id
      // 获取权限列表
      this.permData = listToTreeData(await getPermissionList(), '0')
      // 打开弹窗
      this.showPermDialog = true
      // 获取被点击的角色权限列表
      const { permIds } = await getRoleDetail(id)
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds)
      })
    },
    btnPermCancel() {
      // 清空数据
      this.$refs.permTree.setCheckedKeys([])
      // 关闭弹窗
      this.showPermDialog = false
    },
    async  btnPermOK() {
      // 获取数据 发送请求
      const permIds = this.$refs.permTree.getCheckedKeys()
      const id = this.userId
      await assignPerm({ id, permIds })
      // 提示用户
      this.$message.success('修改权限成功')
      // 关闭弹窗
      this.showPermDialog = false
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.fromData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      console.log(total, rows)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    async deleteRole(id) {
      //  提示
      try {
        await this.$confirm('确认删除该角色吗')
        // 只有点击了确定 才能进入到下方
        await deleteRole(id) // 调用删除接口
        this.getRoleList() // 重新加载数据
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 点击编辑
    async editRole(id) {
      // 回显数据
      this.roleForm = await getRoleDetail(id)
      // 弹出弹框
      this.showDialog = true
    },
    // 点击确认
    async btnOK() {
      // 验证
      await this.$refs.roleForm.validate()
      // 发送请求
      if (this.roleForm.id) {
        // 编辑
        await updateRole(this.roleForm)
      } else {
        // 新增
        await addRole(this.roleForm)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.showDialog = false
      // 重新加载数据
      this.getRoleList()
    },
    // 点击取消
    btnCancel() {
      // 清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清空验证
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
    }

  }

}
</script>

<style>

</style>
